package com.example.book.config;


import com.example.book.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@Slf4j
@ControllerAdvice
@ResponseBody
public class ExceptionAdvice {
    //捕获异常
    @ExceptionHandler
    public Object handler(Exception e){
        log.error("发生异常，e",e);
        return Result.fail();
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler
    //数组越界异常
    public Object handler(ArrayIndexOutOfBoundsException e){
        log.error("发生异常，e",e);
        return Result.fail("发生数组越界异常");
    }
    @ExceptionHandler
    //数组越界异常
    public Object handler(NullPointerException e){
        log.error("发生异常，e",e);
        return Result.fail("发生空指针异常");
    }
}
